// 云函数入口文件
const cloud = require('wx-server-sdk');

cloud.init();

const db = cloud.database();

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    const expiredMembers = await db.collection('user').where({
      futureFormattedDate: event.futureFormattedDate, // 将这里的 event.futureFormattedDate 替换为实际的到期日期
      vip: true
    }).get();

    if (expiredMembers.data.length > 0) {
      const tasks = expiredMembers.data.map(member => {
        return db.collection('user').doc(member._id).update({
          data: {
            vip: false,
            futureFormattedDate: '0'
          }
        });
      });

      await Promise.all(tasks);
    }

    return {
      success: true
    };
  } catch (err) {
    console.error(err);
    return {
      success: false,
      error: err
    };
  }
};
